package com.dj.core.user;

import org.springframework.util.StringUtils;

import com.dj.core.user.api.UserNamePasswordErrorException;
import com.dj.dal.UserDao;
import com.dj.domain.entity.DaiJiaUser;
import com.dj.domain.exception.DaijiaServiceException;
import com.dj.domain.user.LoginUserInfo;

/**
 * 用户登录实现
 * 
 * @author jasen.hsu.cn@gmail.com 2012-4-17 下午9:44:32
 */
public class LoginService {

    private UserDao userDao;

    public DaiJiaUser loginIn(String loginId, String password) throws UserNamePasswordErrorException {
        if (!StringUtils.hasLength(loginId)) {// 用户名为空
            throw new UserNamePasswordErrorException();
        }
        DaiJiaUser user = userDao.getByLoginIdAndSite(loginId, DaiJiaUser.DEFAULT_USER_SITE);
        if (user == null) {
            user = userDao.getByMobileAndSite(loginId);
        }
        if (user == null || !password.equals(user.getPassword())) {
            throw new UserNamePasswordErrorException();
        }
        // LoginUserInfo loginUser = new LoginUserInfo();
        // UserCopier.convert2LoginUser(user, loginUser);
        return user;
    }

    public LoginUserInfo loginInFromExt(String site, String accessToken) throws DaijiaServiceException {
        LoginUserInfo loginUser = new LoginUserInfo();
        loginUser.setSiteToken(accessToken);
        return loginUser;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

}
